package logistics;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.sun.java.swing.plaf.windows.resources.windows;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;

public class register extends JFrame {
  JPanel p1,p2,p3,p4,p5,pboss;
    FlowLayout flowLayout;
	JTextField t1;
	JPasswordField jp;
	JButton jb1;
	JLabel l1,l2,l3;
	//构造方法
	public register(){
		super("后勤登录系统");
		init();
	}
	//初始化方法
	private void init(){
		p1 = new JPanel();
		p2 = new JPanel();
		p3 = new JPanel();
		p4 = new JPanel();
		p5 = new JPanel();
		pboss = new JPanel();
		
		l1 = new JLabel("用 户: ");
		l2 = new JLabel("密 码: ");
		t1 = new JTextField(10);
		jp = new JPasswordField(10);
//		jb1 = new JButton("              登  录              ");
		jb1 = new JButton("   登  录   ");
		jb1.setFont(new Font("宋体", Font.BOLD, 18));
		l3 = new JLabel("后勤系统登录");
		l3.setFont(new java.awt.Font("Dialog",1,30));
		l3.setForeground(Color.blue);      //new Color(0x00ffff)
		
		p3.add(jb1);
		p4.setBackground(new Color(0,200,0));
		p3.setBackground(new Color(200,200,200));
		
		flowLayout = new FlowLayout();
		p1.setLayout(new FlowLayout());
		p2.setLayout(new FlowLayout());
		p5.setLayout(new GridLayout(2,1));
		
		p1.add(l1);
		p1.add(t1);
		p2.add(l2);
		p2.add(jp);
		
		p1.setBackground(new Color(200,200,200));
		p2.setBackground(new Color(200,200,200));
		pboss.setBackground(new Color(200,200,200));
		p5.add(p1);
		p5.add(p2);
		
		pboss.setLayout(new GridLayout(3,1));
		pboss.add(p4);
		GroupLayout gl_p4 = new GroupLayout(p4);
		gl_p4.setHorizontalGroup(
			gl_p4.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_p4.createSequentialGroup()
					.addContainerGap(112, Short.MAX_VALUE)
					.addComponent(l3)
					.addGap(110))
		);
		gl_p4.setVerticalGroup(
			gl_p4.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_p4.createSequentialGroup()
					.addGap(26)
					.addComponent(l3)
					.addContainerGap(29, Short.MAX_VALUE))
		);
		p4.setLayout(gl_p4);
		pboss.add(p5);
		pboss.add(p3);
		// 输入密码后回车相当于点击了登录按钮
		getRootPane().setDefaultButton(jb1);
		getContentPane().add(pboss);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		this.setSize(430, 330);
		this.setLocation((screenSize.width-this.getWidth())/2,
				(screenSize.height-this.getHeight())/2);
		this.setVisible(true);
		this.setAlwaysOnTop(true);
		
		//"登录"按钮侦听器
		jb1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(true){
					String user = t1.getText();
					String password = jp.getText();
					t1.setText("");
					jp.setText("");
					//验证用户及密码
					if(login(user,password)){
						//隐藏登录界面
						register.this.hide();
						//进入功能界面
						new function_show();
					}
				}
			}
		});
		
		
	}
	//用户及密码确认：
	private boolean login(String user, String password){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "";
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521/orcl","Amy","123");
			stmt = conn.createStatement();
			sql = "select password from logistics_staff where Name='"+user+"'";
			rs = stmt.executeQuery(sql);
			//如果用户名不存在：
			if(!rs.next()){
				prompt();
				return false;
			}
			//匹配用户的密码：
			else{
				String pass = rs.getNString("password");
				if(password.equals(pass)){
					return true;
					//进入功能界面
					
				}else{
					prompt();
					return false;
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		finally {
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(stmt!=null){
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	
	private void prompt(){
		
		JOptionPane.showMessageDialog(this, "用户名或密码错误\n      请重新输入!");
	}
}